import json
from typing import Literal


def to_str(o, otype: Literal['compact', 'display']='compact', indent=4) -> str:
    """Convert object to json formatted string.
    
    Args:
        o: an object
        otype: operation type, `compact` or `display`
        indent: useful when otype is `display`

    Returns:
        a json formatted string
    """
    if otype == 'compact':
        return json.dumps(o, ensure_ascii=False, separators=(',', ':'))
    if otype == 'display':
        return json.dumps(o, ensure_ascii=False, indent=indent)
    raise ValueError(f"Unsupported type {otype}")


def str2dict(s: str) -> dict:
    """Convert json formatted string to dict"""
    try:
        j = json.loads(s)
    except:
        j = eval(s)
    return j
